package com.lemon.smartsafety.ui.activity.notice;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.notice.NoticeAddEvent;
import com.lemon.smartsafetynew.ui.activity.base.BaseSearchFragmentActivity;
import com.lemon.smartsafety.ui.fragment.notice.NoticeListFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;

/**
 * Created by Cooliean on 16/4/8.
 */
public class NoticeListActivity extends BaseSearchFragmentActivity {

    private NoticeListFragment mFragment;
    private String sHasRead = "0";
    private String mKeyWord = "";


    @Override
    public Fragment getFragment() {
        mFragment = NoticeListFragment.newInstance(mKeyWord, sHasRead);
        return mFragment;
    }


    public Fragment getFragment(String text, String hasRead) {
        mFragment = NoticeListFragment.newInstance(text, hasRead);
        return mFragment;
    }


    @Override
    public int getSearchHintRes() {
        return R.string.search_common_hit;
    }

    @Override
    public boolean onQueryTextSubmit(String queryText) {
        mKeyWord = StringUtils.nullValue(queryText);
        replaceFragment(R.id.main_content, getFragment(mKeyWord, sHasRead));
        return super.onQueryTextSubmit(queryText);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean menuResult = super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_notice, menu);
        return menuResult;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        OkHttpUtils.getInstance().cancelTag(EventTag.TAG_NOTICE_LIST + sHasRead);//去掉之前的请求
        if (item.getItemId() == R.id.action_unread) {
            sHasRead = "0";
        } else if (item.getItemId() == R.id.action_readed) {
            sHasRead = "1";
        } else if (item.getItemId() == R.id.action_all) {
            sHasRead = "";
        }
        if(item.getItemId() == R.id.action_unread || item.getItemId() == R.id.action_readed || item.getItemId() == R.id.action_all){
            mKeyWord = "";
            showSearch(false);
            replaceFragment(R.id.main_content, getFragment(mKeyWord, sHasRead));
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void initTitle() {
        setBarTitle("通知公告");
    }


    @Override
    protected void callFinish() {
        mFragment = null;
        AppManager.getAppManager().finishActivity(NoticeListActivity.class);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            callFinish();
        }
        return super.onKeyDown(keyCode, event);
    }
    @Subscribe
    public void onEventMainThread(NoticeAddEvent event) {
        Bundle bundle = new Bundle();
        UIHelper.showActivity(this, bundle, NoticePropActivity.class);
    }
}
